﻿//=============================================================================
// Project  : NIPO Component Framework
// File    : Logger.cs
// Author  : Bernhard Dieber (Bernhard.Dieber@uni-klu.ac.at)
// Copyright 2010 by Bernhard Dieber
// This code is published under the Microsoft Public License (Ms-PL).  A copy
// of the license should be distributed with the code.  It can also be found
// at the project website: http://NIPO.CodePlex.com.   This notice, the
// author's name, and all copyright notices must remain intact in all
// applications, documentation, and source files.
//=============================================================================

using System.IO;
using log4net.Appender;
using log4net.Config;

namespace NIPO.Util
{

    /// <summary>
    /// Provides logging functionality on multiple levels.
    /// </summary>
    public class Logger
    {
        private readonly log4net.ILog _log;

        /// <summary>
        /// Inits logging for logging to the file specified by <paramref name="path"/> <br />
        /// Sets loglevel to All
        /// </summary>
        /// <param name="path">Path to logfile</param>
        public static void InitLogging(FileInfo path)
        {
            FileAppender appender = new FileAppender
                                        {
                                            Layout =
                                                new log4net.Layout.PatternLayout(
                                                "%level - %logger - %date{HH:mm:ss,fff}: %m %newline"),
                                            File = path.FullName,
                                            Threshold = log4net.Core.Level.All
                                        };
            BasicConfigurator.Configure(appender);
        }   

        ///<summary>
        /// Inits logging for console output.
        /// Log level is set to All
        ///</summary>
        public static void InitLogging()
        {
            ConsoleAppender appender = new ConsoleAppender
            {
                Layout =
                new log4net.Layout.PatternLayout(
                    "%level - %logger - %date{HH:mm:ss,fff}: %m %newline"),
                Threshold = log4net.Core.Level.All
            };
            BasicConfigurator.Configure(appender);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Logger"/> class.
        /// </summary>
        /// <param name="name">The logger name to qualify the logger output</param>
        internal Logger(string name)
        {
            _log = log4net.LogManager.GetLogger(name);
        }

        /// <summary>
        /// Writes a plain debug message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void WriteDebug(object message)
        {
            if (_log.IsDebugEnabled)
                _log.Debug(message);
        }

        /// <summary>
        /// Writes a formatted debug message with parameters
        /// </summary>
        /// <param name="format">The format.</param>
        /// <param name="parameters">The parameters.</param>
        public void WriteDebugFormat(string format, params object[] parameters)
        {
            if (_log.IsDebugEnabled)
                _log.DebugFormat(format, parameters);
        }
        /// <summary>
        /// Writes a plain info message.
        /// </summary>
        /// <param name="message">The message.</param>

        public void WriteInfo(object message)
        {
            if (_log.IsInfoEnabled)
                _log.Info(message);
        }
        /// <summary>
        /// Writes a formatted info message with parameters
        /// </summary>
        /// <param name="format">The format.</param>
        /// <param name="parameters">The parameters.</param>
        public void WriteInfoFormat(string format, params object[] parameters)
        {
            if (_log.IsInfoEnabled)
                _log.InfoFormat(format, parameters);
        }
        /// <summary>
        /// Writes a plain warning message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void WriteWarning(object message)
        {
            if (_log.IsWarnEnabled)
                _log.Warn(message);
        }
        /// <summary>
        /// Writes a formatted warning message with parameters
        /// </summary>
        /// <param name="format">The format.</param>
        /// <param name="parameters">The parameters.</param>
        public void WriteWarningFormat(string format, params object[] parameters)
        {
            if (_log.IsWarnEnabled)
                _log.WarnFormat(format, parameters);
        }
        /// <summary>
        /// Writes a plain error message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void WriteError(object message)
        {
            if (_log.IsErrorEnabled)
                _log.Error(message);
        }
        /// <summary>
        /// Writes a formatted error message with parameters
        /// </summary>
        /// <param name="format">The format.</param>
        /// <param name="parameters">The parameters.</param>
        public void WriteErrorFormat(string format, params object[] parameters)
        {
            if (_log.IsErrorEnabled)
                _log.ErrorFormat(format, parameters);
        }
        /// <summary>
        /// Writes a plain fatal message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void WriteFatal(object message)
        {
            if (_log.IsFatalEnabled)
                _log.Fatal(message);
        }
        /// <summary>
        /// Writes a formatted fatal message with parameters
        /// </summary>
        /// <param name="format">The format.</param>
        /// <param name="parameters">The parameters.</param>
        public void WriteFatalFormat(string format, params object[] parameters)
        {
            if (_log.IsFatalEnabled)
                _log.FatalFormat(format, parameters);
        }
    }
}